#!/bin/sh

# Slackware build script for suPHP

# Written by Menno E. Duursma <druiloor@zonnet.nl>

# Exit on most errors
set -e

PRGNAM=suphp
VERSION=0.6.2
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}	# Drop the package in /tmp

# The stock Apache on Slackware runs httpd onder system
# user/group account 'apache'. If you happen to use some
# other account (which should improve security) change below
# and make sure /etc/apache/suphp.conf matches
HTTPD_USER=apache
HTTPD_GROUP=apache

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar -xzvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# Apply patch to have it globally honor the suPHP_Engine directive
cat $CWD/suphp-0.6.2-vhosts.patch | patch -p0 --verbose

# Default to secure settings, as any of the configuretion options 
# can be overwritten in the config-file /etc/apache/suphp.conf anyways
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --with-apr=/usr/bin/apr-1-config \
  --with-apxs=/usr/sbin/apxs \
  --sysconfdir=/etc/httpd \
  --with-apache-user=$HTTPD_USER \
  --with-logfile=/var/log/apache/suphp_log

# Compile the application and install it into the $PKG directory
make
make install-strip DESTDIR=$PKG

# The above misses Apache module
( cd $PKG
    strip --strip-unneeded usr/libexec/apache/mod_suphp.so
)

# Copy program documentation into the package
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS COPYING ChangeLog doc/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# Add a sample configuration file 
mkdir -p $PKG/etc/httpd
cat $CWD/suphp.conf \
  | tr 'webserver_user=apache' "webserver_user=$HTTPD_USER" \
    >> $PKG/etc/httpd/suphp.conf.new

# Copy the slack-desc (and a custom doinst.sh if necessary) into ./install
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# Make sure the access permissions on target host are such that
# only the group Apache runs as has access to it
echo "chgrp $HTTPD_GROUP usr/sbin/suphp" >> $PKG/install/doinst.sh
echo "chmod 4750 usr/sbin/suphp" >> $PKG/install/doinst.sh

# Make the package; be sure to leave it in $OUTPUT
cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz

# Clean up the extra stuff
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$PRGNAM-$VERSION
  rm -rf $PKG
fi

